package com.agileai.portal.controller.forum;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import com.agileai.common.KeyGenerator;
import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.annotation.PageAction;
import com.agileai.hotweb.controller.core.SimpleHandler;
import com.agileai.hotweb.domain.core.User;
import com.agileai.hotweb.renders.AjaxRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.portal.bizmoduler.forum.ForumConst;
import com.agileai.portal.bizmoduler.forum.ForumSectionInfoTreeManage;
import com.agileai.portal.bizmoduler.forum.PostManage;
import com.agileai.portal.bizmoduler.forum.UserLevel8BBSUserInfoManage;
import com.agileai.portal.extend.forum.ForumPrivilegeInterceptor;
import com.agileai.portal.notice.PortalMessageSender;
import com.agileai.util.DateUtil;
import com.agileai.util.StringUtil;

/**
 * generated by miscdp
 */
public class ForumProviderHandler extends SimpleHandler{
	public static String REWARD_POINTS = "";
	private static String ORDINARYPOSTS = "ORDINARY_POSTS";
	private static String Y = "Y";
	private static String N = "N";
	public ForumProviderHandler(){
		super();
	}
	
	public ViewRenderer prepareDisplay(DataParam param) {
		String responseText = "";
		String moduleId =  param.get("moduleId");
		JSONObject jsonObject = new JSONObject();
		ForumSectionInfoTreeManage forumSectionInfoTreeManage = this.lookupService(ForumSectionInfoTreeManage.class);
		List<DataRow> records = forumSectionInfoTreeManage.findForumSectionInfoRecords(moduleId);
		List<DataRow> moderatorRecords = forumSectionInfoTreeManage.findModeratorInfoRecords(moduleId);
		try {
			JSONArray jsonArray = new JSONArray();
			jsonObject.put("moduleInfos", jsonArray);
			for (int i=0;i < records.size();i++){
				DataRow row = records.get(i);
				JSONObject tempJsonObject = new JSONObject();
				String fciId = row.stringValue("FCI_ID");
				String fciCode = row.stringValue("FCI_CODE");
				String fciName = row.stringValue("FCI_NAME");
				String moderatorNames = " ";
				String newCreateTime = "";
				String fciMotto = row.stringValue("FCI_MOTTO");
				String menuCode = "";
				JSONObject jsObject = new JSONObject(fciMotto);
				menuCode = (String) jsObject.get("menuCode");
				menuCode = "/portal/request/05/"+menuCode+".ptml";
				String sectionId = row.stringValue("FCI_ID");
				DataRow dataRow = forumSectionInfoTreeManage.getSectionInfo(sectionId,"All","All");
				int postNum = forumSectionInfoTreeManage.getReplyPostCount(sectionId);
				int themeNum = dataRow.getInt("FPM_COUNTS");
				for(int j=0;j<moderatorRecords.size();j++){
					DataRow moderatorRow = moderatorRecords.get(j);
					String fciIdValue = moderatorRow.stringValue("FCI_ID");
					if(fciId.equals(fciIdValue)){
						String moderatorName = moderatorRow.getString("FU_NAME");
						moderatorNames = moderatorName +"  " +moderatorNames;
					}
				}
				
				Date fpmCreateTime = (Date) row.get("FPM_CREATE_TIME");
				Date friTime = (Date) row.get("FRI_TIME");
				if(null != fpmCreateTime){
					if(null == friTime || fpmCreateTime.after(friTime)){
						newCreateTime = DateUtil.getDateByType(5, fpmCreateTime);
					}else{
						newCreateTime = DateUtil.getDateByType(5, friTime);
					} 
				}
					 
				jsonArray.put(tempJsonObject);
				tempJsonObject.put("id", fciId);
				tempJsonObject.put("menuCode", menuCode);
				tempJsonObject.put("name", fciName);
				tempJsonObject.put("fciCode", fciCode);
				tempJsonObject.put("moderatorNames", moderatorNames);
				tempJsonObject.put("newCreateTime", newCreateTime);
				tempJsonObject.put("postNum", postNum);
				tempJsonObject.put("themeNum", themeNum);
				
			}
			String content = jsonObject.toString();
			responseText = content;
			
			this.setAttribute("content",jsonObject.toString());
		} catch (Exception e) {
			this.log.error(e.getLocalizedMessage(), e);
		}
		return new AjaxRenderer(responseText);
	}
	
	@PageAction
	public ViewRenderer findPostGrid(DataParam param) {
		String responseText = null;
		String moduleId =  param.get("moduleId");
		String sectionId =  param.get("sectionId");
		String postTypeCode =  param.get("postTypeCode");
		String postTypeId =  param.get("postTypeId");
		JSONObject jsonObject = new JSONObject();
		int count = 0;
		List<DataRow> records = null;
		PostManage postManage = this.lookupService(PostManage.class);
		ForumSectionInfoTreeManage forumSectionInfoTreeManage = this.lookupService(ForumSectionInfoTreeManage.class);
		List<DataRow> tempRecords = postManage.findPostRecords(moduleId,postTypeCode,postTypeId);
		DataRow dataRow = forumSectionInfoTreeManage.getSectionInfo(sectionId,postTypeCode,postTypeId);
		records = tempRecords;
		count = records.size();
		try {
			if (count > 0){
				JSONArray jsonArray = new JSONArray();
				jsonObject.put("infomations", jsonArray);
				jsonObject.put("infomationCounts", String.valueOf(dataRow.getInt("FPM_COUNTS")));
				for (int i=0;i < count;i++){
					DataRow row = records.get(i);
					if (StringUtil.isNotNullNotEmpty(moduleId) &&  i == 0){
						String moduleName = row.stringValue("FCI_NAME");
						jsonObject.put("moduleId", moduleId);
						jsonObject.put("moduleName", moduleName);
					}
					JSONObject tempJsonObject = new JSONObject();
					String postId = row.stringValue("FPM_ID");
					String postTitle = row.stringValue("FPM_TITLE");
					String readCount = row.stringValue("FPM_CLICK_NUMBER");
					String repliesCount = this.getRepliesCount(postId);
					String postCreator = row.stringValue("FPM_USERNAME");
					String postType = row.stringValue("FPM_TYPE");
					String postExpression = row.stringValue("FPM_EXPRESSION");
					String fpmTop = row.stringValue("FPM_IS_TOP");
					String fpmSectionTop = row.stringValue("FPM_SECTION_TOP");
					Date createTime = (Date)row.get("FPM_CREATE_TIME");
					jsonArray.put(tempJsonObject);
					tempJsonObject.put("id", postId);
					tempJsonObject.put("title", postTitle);
					tempJsonObject.put("repliesCount", repliesCount);
					tempJsonObject.put("readCount", readCount);
					tempJsonObject.put("postType", postType);
					tempJsonObject.put("postExpression", postExpression);
					tempJsonObject.put("postCreator", postCreator);
					tempJsonObject.put("createTime", DateUtil.getDateByType(DateUtil.YYMMDDHHMI_SLANTING,createTime));
					tempJsonObject.put("fpmTop", fpmTop);
					tempJsonObject.put("fpmSectionTop", fpmSectionTop);
					User user = (User) this.getUser();
					if(ForumPrivilegeInterceptor.isForumAdmin(user)){
						tempJsonObject.put("isAdmin", "YES");
					}else{
						tempJsonObject.put("isAdmin", "NO");
					}
					if(ForumPrivilegeInterceptor.isForumAdmin(user)||ForumPrivilegeInterceptor.isModerator(user, moduleId)){
						tempJsonObject.put("isAdmin8Moderator", "YES");
					}else{
						tempJsonObject.put("isAdmin8Moderator", "NO");
					}
				}
			}else{
				JSONArray jsonArray = new JSONArray();
				jsonObject.put("infomations", jsonArray);
				jsonObject.put("infomationCounts", "0");
			}
			this.setAttribute("content",jsonObject.toString());
			
			responseText = jsonObject.toString();
		} catch (Exception e) {
			this.log.error(e.getLocalizedMessage(), e);
		}
		return new AjaxRenderer(responseText);
	}
	
	public String getRepliesCount(String postId) {
		String count = "0";
		PostManage postManage = this.lookupService(PostManage.class);
		DataRow record = postManage.retrieveRepliesCount(postId);
		count = String.valueOf(record.getInt("RepliesCount"));
		return count;
	}
	
	@SuppressWarnings("rawtypes")
	@PageAction
	public ViewRenderer findFavPostGrids(DataParam param) {
		String responseText = null;
		User user = (User) this.getUser();
		HashMap userForumProperties = (HashMap) user.getExtendProperties().get("_ForumPrivlege_");
		String userId = (String) userForumProperties.get("FU_ID");
		JSONObject jsonObject = new JSONObject();
		int count = 0;
		List<DataRow> records = null;
		PostManage postManage = this.lookupService(PostManage.class);
		List<DataRow> tempRecords = postManage.findFavPostGrids(userId);
		records = tempRecords;
		count = records.size();
		try {
			if (count > 0){
				JSONArray jsonArray = new JSONArray();
				jsonObject.put("infomations", jsonArray);
				Integer countResult = postManage.getFavCount(userId);
				String counts = String.valueOf(countResult);
				jsonObject.put("topTitle", counts);
				for (int i=0;i < count;i++){
					DataRow row = records.get(i);
					JSONObject tempJsonObject = new JSONObject();
					String postId = row.stringValue("FPM_ID");
					String postTitle = row.stringValue("FPM_TITLE");
					String readCount = row.stringValue("FPM_CLICK_NUMBER");
					String repliesCount = this.getRepliesCount(postId);
					String postCreator = row.stringValue("FPM_USERNAME");
					String ffId = row.stringValue("FF_ID");
					Date createTime = (Date)row.get("FPM_CREATE_TIME");
					jsonArray.put(tempJsonObject);
					tempJsonObject.put("id", postId);
					tempJsonObject.put("title", postTitle);
					tempJsonObject.put("readCount", readCount);
					tempJsonObject.put("repliesCount", repliesCount);
					tempJsonObject.put("postCreator", postCreator);
					tempJsonObject.put("ffId", ffId);
					tempJsonObject.put("createTime", DateUtil.getDateByType(DateUtil.YYMMDDHHMI_SLANTING,createTime));
				}
			}else{
				JSONArray jsonArray = new JSONArray();
				jsonObject.put("infomations", jsonArray);
				Integer countResult = postManage.getFavCount(userId);
				String counts = String.valueOf(countResult);
				jsonObject.put("topTitle", counts);
				}
			this.setAttribute("content",jsonObject.toString());
			
			responseText = jsonObject.toString();
		} catch (Exception e) {
			this.log.error(e.getLocalizedMessage(), e);
		}
		return new AjaxRenderer(responseText);
	}
	
	@PageAction
	public ViewRenderer addLoginCount(DataParam param){
		String responseText = FAIL;
		try {
			User user = (User) this.getUser();
			ForumPrivilegeInterceptor.increaseUserLoginCount(user);
			this.upUserLevel(user);
			responseText = SUCCESS;
		} catch (Exception e) {
			this.log.error(e.getLocalizedMessage(), e);
		}
		return new AjaxRenderer(responseText);
	}
	
	public void upUserLevel(User user){
		//登陆次数
		String strLoginNums = ForumPrivilegeInterceptor.getFuLoginNums(user);
		//帖子数
		UserLevel8BBSUserInfoManage userLevel8BBSUserInfoManage = this.lookupService(UserLevel8BBSUserInfoManage.class);
		String fuId = ForumPrivilegeInterceptor.getFuId(user);
		DataParam param1 = new DataParam("fuId",fuId);
		DataRow row1 = userLevel8BBSUserInfoManage.retrieveForumUserPostNums(param1);
		String strPostNums = String.valueOf(row1.get("postNums"));
		//精华帖+重点贴
		String postType = "'ESSENCE_POSTS','KEY_POSTS'";
		DataParam param2 = new DataParam("fuId",fuId,"postType",postType);
		DataRow row2 = userLevel8BBSUserInfoManage.retrieveForumUserPostNums(param2);
		String strKeyPostNums = String.valueOf(row2.get("postNums"));
		
		Integer loginNums = Integer.valueOf(strLoginNums);
		Integer postNums = Integer.valueOf(strPostNums);
		Integer keyPostNums = Integer.valueOf(strKeyPostNums);
		
		String fuLevel = ForumPrivilegeInterceptor.getFuLevel(user);
		DataParam upLevelParam = new DataParam("fuCode",ForumPrivilegeInterceptor.getFuCode(user)); 
		if(ForumConst.SOLDIER.equals(fuLevel)){
			if(loginNums>=50&&postNums>=10&&keyPostNums>=1){
				upLevelParam.put("level", "SQUAD_LEADER");
				userLevel8BBSUserInfoManage.updateUserLevel(upLevelParam);
			}
		}else if(ForumConst.SQUAD_LEADER.equals(fuLevel)){
			if(loginNums>=100&&postNums>=15&&keyPostNums>=2){
				upLevelParam.put("level", "PLATOON_LEADER");
				userLevel8BBSUserInfoManage.updateUserLevel(upLevelParam);
			}
		}else if(ForumConst.PLATOON_LEADER.equals(fuLevel)){
			if(loginNums>=150&&postNums>=20&&keyPostNums>=4){
				upLevelParam.put("level", "COMPANY_COMMANDER");
				userLevel8BBSUserInfoManage.updateUserLevel(upLevelParam);
			}			
		}else if(ForumConst.COMPANY_COMMANDER.equals(fuLevel)){
			if(loginNums>=200&&postNums>=25&&keyPostNums>=6){
				upLevelParam.put("level", "BATTALION_COMMANDER");
				userLevel8BBSUserInfoManage.updateUserLevel(upLevelParam);
			}			
		}else if(ForumConst.BATTALION_COMMANDER.equals(fuLevel)){
			if(loginNums>=250&&postNums>=30&&keyPostNums>=8){
				upLevelParam.put("level", "REGIMENT_COMMANDER");
				userLevel8BBSUserInfoManage.updateUserLevel(upLevelParam);
			}			
		}else if(ForumConst.REGIMENT_COMMANDER.equals(fuLevel)){
			if(loginNums>=300&&postNums>=35&&keyPostNums>=10){
				upLevelParam.put("level", "BRIGADE_COMMANDER");
				userLevel8BBSUserInfoManage.updateUserLevel(upLevelParam);
			}			
		}else if(ForumConst.BRIGADE_COMMANDER.equals(fuLevel)){
			if(loginNums>=350&&postNums>=40&&keyPostNums>=12){
				upLevelParam.put("level", "DIVISION_COMMANDER");
				userLevel8BBSUserInfoManage.updateUserLevel(upLevelParam);
			}			
		}else if(ForumConst.DIVISION_COMMANDER.equals(fuLevel)){
			if(loginNums>=400&&postNums>=45&&keyPostNums>=14){
				upLevelParam.put("level", "CORPS_COMMANDER");
				userLevel8BBSUserInfoManage.updateUserLevel(upLevelParam);
			}			     
		}else if(ForumConst.CORPS_COMMANDER.equals(fuLevel)){
			if(loginNums>=500&&postNums>=55&&keyPostNums>=20){
				upLevelParam.put("level", "REGION_COMMANDER");
				userLevel8BBSUserInfoManage.updateUserLevel(upLevelParam);
			}			
		}else{
			//donothing
		}
	}
	
	@PageAction
	public ViewRenderer saveCollection(DataParam param) {
		String responseText = FAIL;
		try {
			String fpmId = param.getString("fpmId");
			User user = (User) this.getUser();
			String fuId = ForumPrivilegeInterceptor.getFuId(user);
			ForumSectionInfoTreeManage forumSectionInfoTreeManage = this.lookupService(ForumSectionInfoTreeManage.class);
			int ffId = forumSectionInfoTreeManage.getForumFavoriteRecord(fpmId,fuId);
			if(ffId == 0){
				String fpmTitle = forumSectionInfoTreeManage.getFpmTitleRecord(fpmId);
				forumSectionInfoTreeManage.createForumFavoriteRecord(fpmId,fuId,fpmTitle);
				responseText = SUCCESS;
			}
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		
		return new AjaxRenderer(responseText);
	}
	
	@PageAction
	public ViewRenderer sharePoints(DataParam param) {
		String responseText = FAIL;
		try {
			String fpmId = param.getString("fpmId");
			String friId = param.getString("friId");
			Date friTime = DateUtil.getDateTime(param.get("friTime"));
			String sharePointsValue = param.getString("sharePointsValue");
			ForumSectionInfoTreeManage forumSectionInfoTreeManage = this.lookupService(ForumSectionInfoTreeManage.class);
			DataRow forumPostMessageRow = forumSectionInfoTreeManage.getForumPostMessageRecord(fpmId);
			
			DataRow currentForumRepliesInfoRow = forumSectionInfoTreeManage.getCurrentForumRepliesInfoRecord(friId);
			String fuCode = currentForumRepliesInfoRow.getString("FU_CODE");
			
			String fpmRewardPoints = forumPostMessageRow.getString("FPM_REWARD_POINTS");
			String fpmSharedPoints = forumPostMessageRow.getString("FPM_SHARED_POINTS"); 
			
			if(Integer.valueOf(sharePointsValue) <= Integer.valueOf(fpmRewardPoints)){
				UserLevel8BBSUserInfoManage userLevel8BBSUserInfoManage = this.lookupService(UserLevel8BBSUserInfoManage.class);
				DataRow forumUserRow = userLevel8BBSUserInfoManage.getFuUserInfoRecord(fuCode);
				String fuIntegral = forumUserRow.getString("FU_INTEGRAL");
				String fuLevel = forumUserRow.getString("FU_LEVEL");
				String fuLevelVal = forumSectionInfoTreeManage.getFuLevelValRecord(fuLevel);
				
				DataRow codeListRow =  forumSectionInfoTreeManage.findCodeListRecord(fuLevel);
				String weightStr = codeListRow.getString("CODE_NAME");
				
				int fuIntegralVal = 0;
				fuIntegralVal = Integer.valueOf(fuIntegral) + Integer.valueOf(sharePointsValue) * Integer.valueOf(fuLevelVal) * Integer.valueOf(weightStr);
				if(null == fpmSharedPoints){
					fpmSharedPoints = "0";
				}
				int fpmSharedPointsInt = Integer.valueOf(fpmSharedPoints);
				fpmSharedPointsInt = fpmSharedPointsInt + Integer.valueOf(sharePointsValue);
				fpmSharedPoints = String.valueOf(fpmSharedPointsInt);
				String fuIntegralStr = String.valueOf(fuIntegralVal);
				userLevel8BBSUserInfoManage.updateUserFuIntegralByFuCode(fuCode,fuIntegralStr);
				forumSectionInfoTreeManage.updateFpmSharedPoints(fpmId, fpmSharedPoints);
				forumSectionInfoTreeManage.updateFriIssharepoint(friId,"N");
				forumSectionInfoTreeManage.updateFriTime(friId,friTime);
				responseText = SUCCESS;
			}
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return new AjaxRenderer(responseText);
	}
	
	@PageAction
	public ViewRenderer deleForumPostMessage(DataParam param) throws JSONException {
		String responseText = FAIL;
		try {
			String fpmId = param.getString("fpmId");
			ForumSectionInfoTreeManage forumSectionInfoTreeManage = this.lookupService(ForumSectionInfoTreeManage.class);
			PostManage postManage = this.lookupService(PostManage.class);
			List<DataRow> records = forumSectionInfoTreeManage.findforumRepliesInfoRecords(fpmId);
			if(records.size() == 0){
				DataRow moduleInfoRow = postManage.retrieveModuleInfo(fpmId);
				String fciMotto = moduleInfoRow.getString("FCI_MOTTO");
				JSONObject jsonObject = new JSONObject(fciMotto);
				String menuCode = jsonObject.get("menuCode").toString();
				forumSectionInfoTreeManage.deleForumPostMessage(fpmId);
				responseText = menuCode;
			}
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(),e);
		}
		
		return new AjaxRenderer(responseText);
	}
	
	@PageAction
	public ViewRenderer deleForumPostRepliesInfos(DataParam param) throws JSONException {
		String responseText = FAIL;
		try {
			String fpmId = param.getString("fpmId");
			ForumSectionInfoTreeManage forumSectionInfoTreeManage = this.lookupService(ForumSectionInfoTreeManage.class);
			PostManage postManage = this.lookupService(PostManage.class);
			DataRow moduleInfoRow = postManage.retrieveModuleInfo(fpmId);
			String fciMotto = moduleInfoRow.getString("FCI_MOTTO");
			JSONObject jsonObject = new JSONObject(fciMotto);
			String menuCode = jsonObject.get("menuCode").toString();
			forumSectionInfoTreeManage.deleForumPostMessage(fpmId);
			forumSectionInfoTreeManage.deleForumRepliesInfos(fpmId);
			responseText = menuCode;
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(),e);
		}
		
		return new AjaxRenderer(responseText);
	}
	
	@PageAction
	public ViewRenderer changeFpmType(final DataParam param) {
		String responseText = FAIL;
		try {
			String fpmId = param.get("fpmId");
			String fpmType = param.get("fpmType");
			String fraction = param.get("fraction");
			ForumSectionInfoTreeManage forumSectionInfoTreeManage = this.lookupService(ForumSectionInfoTreeManage.class);
			forumSectionInfoTreeManage.updateFpmTypeRecord(fpmId,fpmType);
			//垃圾帖樓主減分
			DataRow managePostInfoRow = forumSectionInfoTreeManage.getManagePostInfoRecord(fpmId);
			String fpmUserId = managePostInfoRow.getString("FPM_USERID");
			String userCode = managePostInfoRow.getString("FU_CODE");
			UserLevel8BBSUserInfoManage userLevel8BBSUserInfoManage = this.lookupService(UserLevel8BBSUserInfoManage.class);
			DataRow fuIndoRow = userLevel8BBSUserInfoManage.getFuInfoRecord(fpmUserId);
			String fuIntegral = fuIndoRow.getString("FU_INTEGRAL");
			String fuleve = fuIndoRow.getString("FU_LEVEL");
			
			DataRow codeListRow =  forumSectionInfoTreeManage.findCodeListRecord(fuleve);
			String weightStr = codeListRow.getString("CODE_NAME");
			if(!fraction.isEmpty()){
				DataRow fpmTypeRow =  forumSectionInfoTreeManage.findFpmTypeRecord(fraction);
				String fractionStr = fpmTypeRow.getString("CODE_NAME");
				int weight = Integer.valueOf(weightStr);
				int fractionInt = Integer.valueOf(fractionStr);
				int fuIntegralVal = Integer.valueOf(fuIntegral);
				fuIntegralVal = fuIntegralVal + fractionInt * weight;
				String fuIntegralStr = String.valueOf(fuIntegralVal);
				userLevel8BBSUserInfoManage.updateUserFuIntegral(fpmUserId,fuIntegralStr);
			}
			DataRow forumUserRow = userLevel8BBSUserInfoManage.retrieveForumUserRecord(userCode,fpmUserId);
			String mail = forumUserRow.getString("FU_EMAIL");
			param.put("mail", mail);
			param.put("userCode", userCode);
			
			Thread sendMailThread = new Thread() {
				@Override
				public void run() {
					sendMail(param);
				}
			};
			sendMailThread.start();
			
			responseText = SUCCESS;
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}

		return new AjaxRenderer(responseText);
	}
	
	private void sendMail(DataParam param){
		String userCode = param.get("userCode");
		String mail = param.get("mail");
		String title = param.get("fpmTitle");
		String url = param.get("url");
		
		PostManage postManage = this.lookupService(PostManage.class);
		boolean result = postManage.sendNoticeMail(userCode, mail, "GARBAGE", url, title);
		log.info("send mail result is " + result);
	}
	
	@PageAction
	public ViewRenderer changeEndPost(DataParam param) {
		String responseText = FAIL;
		try {
			String fpmId = param.get("fpmId");
			ForumSectionInfoTreeManage forumSectionInfoTreeManage = this.lookupService(ForumSectionInfoTreeManage.class);
			DataRow managePostInfoRow = forumSectionInfoTreeManage.getManagePostInfoRecord(fpmId);
			String fpmExpression = managePostInfoRow.getString("FPM_EXPRESSION_CODE");
			if(ForumConst.DIFFICULT_TO_HELP.equals(fpmExpression) || ForumConst.DIFFICULT_TO_HELP == fpmExpression){
				String fpmRewardPoints = managePostInfoRow.getString("FPM_REWARD_POINTS");
				String fpmSharedPoints = managePostInfoRow.getString("FPM_SHARED_POINTS");
				int mark = Integer.valueOf(fpmRewardPoints) - Integer.valueOf(fpmSharedPoints);
				if(mark == 0){
					forumSectionInfoTreeManage.updateFpmState(fpmId,"HAS_END");
					responseText = SUCCESS;
				}
			}else{
				forumSectionInfoTreeManage.updateFpmState(fpmId,"HAS_END");
				responseText = SUCCESS;
			}
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(),e);
		}
		
		return new AjaxRenderer(responseText);
	}
	
	@PageAction
	public ViewRenderer reportPost(DataParam param) {
		String responseText = FAIL;
		try {
			String fpmId = param.get("fpmId");
			ForumSectionInfoTreeManage forumSectionInfoTreeManage = this.lookupService(ForumSectionInfoTreeManage.class);
			DataRow managePostInfoRow = forumSectionInfoTreeManage.getManagePostInfoRecord(fpmId);

			String fpmUserId = managePostInfoRow.getString("FPM_USERID");
			UserLevel8BBSUserInfoManage userLevel8BBSUserInfoManage = this.lookupService(UserLevel8BBSUserInfoManage.class);
			DataRow fuIndoRow = userLevel8BBSUserInfoManage.getFuInfoRecord(fpmUserId);
			String fuIntegral = fuIndoRow.getString("FU_INTEGRAL");
			String fuleve = fuIndoRow.getString("FU_LEVEL");
			
			DataRow codeListRow =  forumSectionInfoTreeManage.findCodeListRecord(fuleve);
			String weightStr = codeListRow.getString("CODE_NAME");
			
			int weight = Integer.valueOf(weightStr);
			int fractionInt = -3 ;
			int fuIntegralVal = Integer.valueOf(fuIntegral);
			fuIntegralVal = fuIntegralVal + fractionInt * weight;
			
			String fuIntegralStr = String.valueOf(fuIntegralVal);
			userLevel8BBSUserInfoManage.updateUserFuIntegral(fpmUserId,fuIntegralStr);
			forumSectionInfoTreeManage.updateFpmTypeRecord(fpmId,"GARBAGE_POSTS");
			forumSectionInfoTreeManage.updateFpmReport(fpmId,"HAS_CONFIRM_REPORT");
			responseText = SUCCESS;
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return new AjaxRenderer(responseText);
	}
	
	@PageAction
	public ViewRenderer cancelReportPost(DataParam param) {
		String responseText = FAIL;
		try {
			String fpmId = param.get("fpmId");
			ForumSectionInfoTreeManage forumSectionInfoTreeManage = this.lookupService(ForumSectionInfoTreeManage.class);
			DataRow managePostInfoRow = forumSectionInfoTreeManage.getManagePostInfoRecord(fpmId);

			String fpmUserId = managePostInfoRow.getString("FPM_USERID");
			UserLevel8BBSUserInfoManage userLevel8BBSUserInfoManage = this.lookupService(UserLevel8BBSUserInfoManage.class);
			DataRow fuIndoRow = userLevel8BBSUserInfoManage.getFuInfoRecord(fpmUserId);
			String fuIntegral = fuIndoRow.getString("FU_INTEGRAL");
			String fuleve = fuIndoRow.getString("FU_LEVEL");
			
			DataRow codeListRow =  forumSectionInfoTreeManage.findCodeListRecord(fuleve);
			String weightStr = codeListRow.getString("CODE_NAME");
			
			int weight = Integer.valueOf(weightStr);
			int fractionInt = 3 ;
			int fuIntegralVal = Integer.valueOf(fuIntegral);
			fuIntegralVal = fuIntegralVal + fractionInt * weight;
			
			String fuIntegralStr = String.valueOf(fuIntegralVal);
			userLevel8BBSUserInfoManage.updateUserFuIntegral(fpmUserId,fuIntegralStr);
			forumSectionInfoTreeManage.updateFpmTypeRecord(fpmId,"ORDINARY_POSTS");
			forumSectionInfoTreeManage.updateFpmReport(fpmId,"NO_REPORT");
			List<DataRow> records = forumSectionInfoTreeManage.findforumReplrtRecords(fpmId);
			String frrIds ="";
			for(int i=0;i<records.size();i++){
				DataRow row = records.get(i);
				String frrId = row.getString("FRR_ID");
				frrIds = frrId+","+frrIds;
			}
			forumSectionInfoTreeManage.deleForumReplrtRecords(frrIds);
			responseText = SUCCESS;
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return new AjaxRenderer(responseText);
	}
	
	@PageAction
	public ViewRenderer cancelReportReply(DataParam param) {
		String responseText = FAIL;
		try {
			ForumSectionInfoTreeManage forumSectionInfoTreeManage = this.lookupService(ForumSectionInfoTreeManage.class);
			String friId = param.getString("friId");
			String friUserId = param.getString("friUserId");
			
			UserLevel8BBSUserInfoManage userLevel8BBSUserInfoManage = this.lookupService(UserLevel8BBSUserInfoManage.class);
			DataRow fuIndoRow = userLevel8BBSUserInfoManage.getFuInfoRecord(friUserId);
			String fuIntegral = fuIndoRow.getString("FU_INTEGRAL");
			String fuleve = fuIndoRow.getString("FU_LEVEL");
			
			DataRow codeListRow =  forumSectionInfoTreeManage.findCodeListRecord(fuleve);
			String weightStr = codeListRow.getString("CODE_NAME");
			
			int weight = Integer.valueOf(weightStr);
			int fractionInt = -3 ;
			int fuIntegralVal = Integer.valueOf(fuIntegral);
			fuIntegralVal = fuIntegralVal + fractionInt * weight;
			
			String fuIntegralStr = String.valueOf(fuIntegralVal);
			userLevel8BBSUserInfoManage.updateUserFuIntegral(friUserId,fuIntegralStr);
			forumSectionInfoTreeManage.updateFriState(friId,"NO_REPORT");
			
			List<DataRow> records = forumSectionInfoTreeManage.findforumReplrtRecords(friId);
			String frrIds ="";
			for(int i=0;i<records.size();i++){
				DataRow row = records.get(i);
				String frrId = row.getString("FRR_ID");
				frrIds = frrId+","+frrIds;
			}
			forumSectionInfoTreeManage.deleForumReplrtRecords(frrIds);
			responseText = SUCCESS;
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return new AjaxRenderer(responseText);
	}
	
	@PageAction
	public ViewRenderer reportReplyPost(DataParam param) {
		String responseText = FAIL;
		try {
			ForumSectionInfoTreeManage forumSectionInfoTreeManage = this.lookupService(ForumSectionInfoTreeManage.class);
			String friId = param.getString("friId");
			String friUserId = param.getString("friUserId");
			
			UserLevel8BBSUserInfoManage userLevel8BBSUserInfoManage = this.lookupService(UserLevel8BBSUserInfoManage.class);
			DataRow fuIndoRow = userLevel8BBSUserInfoManage.getFuInfoRecord(friUserId);
			String fuIntegral = fuIndoRow.getString("FU_INTEGRAL");
			String fuleve = fuIndoRow.getString("FU_LEVEL");
			
			DataRow codeListRow =  forumSectionInfoTreeManage.findCodeListRecord(fuleve);
			String weightStr = codeListRow.getString("CODE_NAME");
			
			int weight = Integer.valueOf(weightStr);
			int fractionInt = -3 ;
			int fuIntegralVal = Integer.valueOf(fuIntegral);
			fuIntegralVal = fuIntegralVal + fractionInt * weight;
			
			String fuIntegralStr = String.valueOf(fuIntegralVal);
			userLevel8BBSUserInfoManage.updateUserFuIntegral(friUserId,fuIntegralStr);
			forumSectionInfoTreeManage.updateFriState(friId,"HAS_CONFIRM_REPORT");
			responseText = SUCCESS;
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return new AjaxRenderer(responseText);
	}
	@PageAction
	public ViewRenderer replyFloorPost(DataParam param) {
		String responseText = FAIL;
		try {
			ForumSectionInfoTreeManage forumSectionInfoTreeManage = this.lookupService(ForumSectionInfoTreeManage.class);
			String friId = param.getString("friId");
			String floor = param.getString("floor");
			
			UserLevel8BBSUserInfoManage userLevel8BBSUserInfoManage = this.lookupService(UserLevel8BBSUserInfoManage.class);
			DataRow fuIndoRow = userLevel8BBSUserInfoManage.getFuInfoRecord(floor);
			String fuIntegral = fuIndoRow.getString("FU_INTEGRAL");
			String fuleve = fuIndoRow.getString("FU_LEVEL");
			
			DataRow codeListRow =  forumSectionInfoTreeManage.findCodeListRecord(fuleve);
			String weightStr = codeListRow.getString("CODE_NAME");
			
			int weight = Integer.valueOf(weightStr);
			int fractionInt = -3 ;
			int fuIntegralVal = Integer.valueOf(fuIntegral);
			fuIntegralVal = fuIntegralVal + fractionInt * weight;
			
			String fuIntegralStr = String.valueOf(fuIntegralVal);
			userLevel8BBSUserInfoManage.updateUserFuIntegral(floor,fuIntegralStr);
			forumSectionInfoTreeManage.updateFriState(friId,"HAS_REPORT");
			responseText = SUCCESS;
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return new AjaxRenderer(responseText);
	}
	
	@PageAction
	public ViewRenderer saveReplyInfo(final DataParam param) {
		String responseText = FAIL;
		try {
			ForumSectionInfoTreeManage forumSectionInfoTreeManage = this.lookupService(ForumSectionInfoTreeManage.class);
			String fpmId = param.get("FPM_ID");
			String replyFloor = param.get("replyFloor");
			String replySource = param.get("replySourceInfo");
			String friContent = param.get("FRI_CONTENT");
			User user = (User)this.getUser();
			String fuCode = user.getUserCode();
			String fuId = forumSectionInfoTreeManage.getFuIdRecord(fuCode);
			DataParam dataParam = new DataParam();
			dataParam.put("FRI_ID", KeyGenerator.instance().genKey());
			dataParam.put("FRI_CONTENT",friContent);
			dataParam.put("FRI_USERID",fuId);
			dataParam.put("FPM_ID",fpmId);
			if(ForumConst.POST_MESSAGE.equals(replySource) || ForumConst.POST_MESSAGE == replySource){
				dataParam.put("FRI_REPORT_NUM","楼主");
			}else{
				dataParam.put("FRI_REPORT_NUM",replyFloor);
			}
			dataParam.put("FRI_TIME", new Date());
			
			forumSectionInfoTreeManage.createRepliesInfoRecord(dataParam);
			
			List<String> userCodes = new ArrayList<String>();
			DataRow managePostInfoRow = forumSectionInfoTreeManage.getManagePostInfoRecord(fpmId);
			String fpmUserId = managePostInfoRow.getString("FPM_USERID");
			String sectionId = managePostInfoRow.getString("FCI_ID");
			List<DataRow> records = forumSectionInfoTreeManage.findUserRecords(sectionId);
			for (int i = 0; i < records.size(); i++) {
				DataRow row = records.get(i);
				String userCode = row.getString("FU_CODE");
				userCodes.add(userCode);
			}
			param.put("userCode", fuCode);
			param.put("userCodes", userCodes);
			//发送消息
			Thread sendreplyMessageToManger = new Thread() {
				@Override
				public void run() {
					sendReplyMessageToManger(param);
				}
			};
			sendreplyMessageToManger.start();
			
			UserLevel8BBSUserInfoManage userLevel8BBSUserInfoManage = this.lookupService(UserLevel8BBSUserInfoManage.class);
			DataRow forumUserRow = userLevel8BBSUserInfoManage.retrieveForumUserRecord(null,fpmUserId);
			String userCode = forumUserRow.getString("FU_CODE");
			String mail = forumUserRow.getString("FU_EMAIL");
			param.put("mail", mail);
			param.put("userCode", userCode);
			//给楼主发邮件
			Thread sendMailThread = new Thread() {
				@Override
				public void run() {
					sendMailToPostUser(param);
				}
			};
			sendMailThread.start();
			
			responseText = SUCCESS;
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return new AjaxRenderer(responseText);
	}
	
	private void sendReplyMessageToManger(DataParam param){
		String userCode = param.get("userCode");
		String title = "帖子有新回复啦!!，标题为"+param.get("replyPostTitle");
		String postUrl = param.get("RepliesUrl");
		String blank = "_blank";
		String msgContent =	"帖子有新回复啦!!，标题为:"+"<a href="+postUrl+" target="+blank+">"+param.get("replyPostTitle")+"</a>";
		@SuppressWarnings("unchecked")
		List<String> userCodes = param.getList("userCodes");
		PortalMessageSender port = new PortalMessageSender();
		port.sendMessage(userCodes,title,msgContent,userCode);
	}
	
	private void sendMailToPostUser(DataParam param){
		String userCode = param.get("userCode");
		String mail = param.get("mail");
		String title = param.get("fpmTitle");
		String url = param.get("RepliesUrl");
		
		PostManage postManage = this.lookupService(PostManage.class);
		boolean result = postManage.sendNoticeMail(userCode, mail, "GARBAGE", url, title);
		log.info("send mail result is " + result);
	}
	
	@PageAction
	public ViewRenderer topForumPostMessage(DataParam param) {
		String responseText = FAIL;
		try {
			String fpmId = param.get("fpmId");
			PostManage postManage = this.lookupService(PostManage.class);
			postManage.updatePostMessageTop(fpmId,Y);
		} catch (Exception e) {
			this.log.error(e.getMessage(), e);
		}
		responseText = SUCCESS;
		return new AjaxRenderer(responseText);
	}
	@PageAction
	public ViewRenderer cancleTopForumPostMessage(DataParam param) {
		String responseText = FAIL;
		try {
			String fpmId = param.get("fpmId");
			PostManage postManage = this.lookupService(PostManage.class);			
			postManage.updatePostMessageTop(fpmId,N);
		} catch (Exception e) {
			this.log.error(e.getMessage(), e);
		}
		responseText = SUCCESS;
		return new AjaxRenderer(responseText);
	}
	@PageAction
	public ViewRenderer topSectionPostMessage(DataParam param) {
		String responseText = FAIL;
		try {
			String fpmId = param.get("fpmId");
			PostManage postManage = this.lookupService(PostManage.class);			
			postManage.updateSectionPostMessageTop(fpmId,Y);
		} catch (Exception e) {
			this.log.error(e.getMessage(), e);
		}
		responseText = SUCCESS;
		return new AjaxRenderer(responseText);
	}
	@PageAction
	public ViewRenderer cancleTopSectionPostMessage(DataParam param) {
		String responseText = FAIL;
		try {
			String fpmId = param.get("fpmId");
			PostManage postManage = this.lookupService(PostManage.class);			
			postManage.updateSectionPostMessageTop(fpmId,N);
		} catch (Exception e) {
			this.log.error(e.getMessage(), e);
		}
		responseText = SUCCESS;
		return new AjaxRenderer(responseText);
	}
	
	@PageAction
	public ViewRenderer createPostInfo(final DataParam param) throws JSONException {
		String responseText = FAIL;
		try {
			ForumSectionInfoTreeManage forumSectionInfoTreeManage = this.lookupService(ForumSectionInfoTreeManage.class);
			User user = (User)this.getUser();
			String fuCode = user.getUserCode();
			String fuId = forumSectionInfoTreeManage.getFuIdRecord(fuCode);
			String fciId = param.get("fciName");
			String fpmRewardPoints = param.get("FPM_REWARD_POINTS");
			
			String fciMotto = forumSectionInfoTreeManage.getFciMottoRecord(fciId);
			JSONObject jsObject = new JSONObject(fciMotto);
			String menuCode = (String) jsObject.get("menuCode");
			
			String fuLevel = ForumPrivilegeInterceptor.getFuLevel(user);
			String fuIntegral = ForumPrivilegeInterceptor.getFuIntegral(user);
			String fuLevelVal = forumSectionInfoTreeManage.getFuLevelValRecord(fuLevel);
			String postScores = forumSectionInfoTreeManage.getPostScores("INTEGRAL_CHANGE","POSTINGS");
			
			int fuIntegralVal = 0;
			if("".equals(fpmRewardPoints) ||  null == fpmRewardPoints){
				fuIntegralVal = Integer.valueOf(fuIntegral) + Integer.valueOf(fuLevelVal) * Integer.valueOf(postScores);
			}else{
				int fpmRewardPointsNum = Integer.valueOf(fpmRewardPoints);
				fuIntegralVal = Integer.valueOf(fuIntegral) + Integer.valueOf(fuLevelVal) * Integer.valueOf(postScores)  - fpmRewardPointsNum;
			}
			
			String fuIntegralStr = String.valueOf(fuIntegralVal);
			UserLevel8BBSUserInfoManage userLevel8BBSUserInfoManage = this.lookupService(UserLevel8BBSUserInfoManage.class);
			userLevel8BBSUserInfoManage.updateUserFuIntegral(fuId,fuIntegralStr);
			
			DataParam dataParam = new DataParam();
			String fpmId = KeyGenerator.instance().genKey();
			dataParam.put("FPM_ID",fpmId );
			dataParam.put("FCI_ID",fciId);
			dataParam.put("FPM_USERID",fuId);
			dataParam.put("FPM_EXPRESSION",param.get("fpm_expression"));
			dataParam.put("FPM_TITLE",param.get("FPM_TITLE"));
			dataParam.put("FPM_CONTENT",param.get("FPM_CONTENT"));
			dataParam.put("FPM_CREATE_TIME", new Date());
			dataParam.put("FPM_CLICK_NUMBER", 0);
			dataParam.put("FPM_REWARD_POINTS", fpmRewardPoints);
			dataParam.put("FPM_IS_TOP", N);
			dataParam.put("FPM_SECTION_TOP", N);
			dataParam.put("FPM_TYPE", ORDINARYPOSTS);
			forumSectionInfoTreeManage.createPostMessageRecord(dataParam);
			
			List<String> userCodes = new ArrayList<String>();
			List<DataRow> records = forumSectionInfoTreeManage.findUserRecords(fciId);
			for (int i = 0; i < records.size(); i++) {
				DataRow row = records.get(i);
				String userCode = row.getString("FU_CODE");
				userCodes.add(userCode);
			}
			param.put("userCode", fuCode);
			param.put("userCodes", userCodes);
			param.put("fpmId", fpmId);
			//发送消息
			Thread sendMessageToManger = new Thread() {
				@Override
				public void run() {
					sendMessageToManger(param);
				}
			};
			sendMessageToManger.start();
			
			responseText = menuCode;
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return new AjaxRenderer(responseText);
	}
	
	private void sendMessageToManger(DataParam param){
		String userCode = param.get("userCode");
		String title = "版块有新帖子啦!!，标题为"+param.get("FPM_TITLE");
		String postUrl = "/portal/request/05/forum-info-page.ptml?contentId="+param.get("fpmId");
		String blank = "_blank";
		String msgContent =	"版块有新帖子啦!!标题为:"+"<a href="+postUrl+" target="+blank+">"+param.get("FPM_TITLE")+"</a>";
		@SuppressWarnings("unchecked")
		List<String> userCodes = param.getList("userCodes");
		PortalMessageSender port = new PortalMessageSender();
		port.sendMessage(userCodes,title,msgContent,userCode);
	}
	
	@PageAction
	public ViewRenderer checkPostInfo(DataParam param) throws JSONException {
		String responseText = FAIL;
		try {
			ForumSectionInfoTreeManage forumSectionInfoTreeManage = this.lookupService(ForumSectionInfoTreeManage.class);
			User user = (User)this.getUser();
			String fuCode = user.getUserCode();
			String fpmRewardPoints = param.get("FPM_REWARD_POINTS");
			
			String fuIntegral = ForumPrivilegeInterceptor.getFuIntegral(user);
			int overIntegral = 0;
			if("" !=fpmRewardPoints && null != fpmRewardPoints ){
				overIntegral = Integer.valueOf(fpmRewardPoints) - Integer.valueOf(fuIntegral);
			}
			String fpmContent = param.get("FPM_CONTENT");
			List<DataRow> shieldWordsList= forumSectionInfoTreeManage.findShieldWordsList();
			boolean containShieldWords = false;
			for(int i=0;i<shieldWordsList.size();i++){
				DataRow shieldWordsRow = shieldWordsList.get(i);
				String shieldWords = shieldWordsRow.getString("CODE_DESC");
				JSONObject jsonObject = new JSONObject(shieldWords);
				String shieldWord = "";
				if(jsonObject.length()>0){
					  for(int j=0;j<jsonObject.length();j++){
						  String shield = "shieldWords"+String.valueOf(j);
						  shieldWord = (String) jsonObject.get(shield);
						  containShieldWords = fpmContent.contains(shieldWord);
						  if(containShieldWords){
							  break;
						  }
					  }
				}
			}
			
			DataRow maxFpmCreateTimeRow = forumSectionInfoTreeManage.getmaxFpmCreateTime(fuCode);
			Date maxFpmCreateTime = maxFpmCreateTimeRow.getTimestamp("MAX_FPM_CREATE_TIME");
			long min = 3;
			if(null != maxFpmCreateTime){
				Date date = new Date();
				long timeMax = maxFpmCreateTime.getTime();  
		        long timeNew = date.getTime(); 
		        long day = (timeNew - timeMax)  / (24 * 60 * 60 * 1000);  
		        long hour = ((timeNew - timeMax) / (60 * 60 * 1000) - day * 24); 
		        min = (((timeNew - timeMax) / (60 * 1000)) - day * 24 * 60 - hour * 60); 
			}
			
			if (containShieldWords){
				responseText = "shieldWords";
			}else if(overIntegral > 0){
				responseText = "overIntegral";
			}else if(min < 3){
				responseText = "isContinuiPostInfo";
			}else{
				responseText = SUCCESS;
			}
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return new AjaxRenderer(responseText);
	}
	
	@PageAction
	public ViewRenderer isSharePoint(DataParam param) {
		String responseText = FAIL;
		try {
			String friId = param.get("friId");
			Date friTime = DateUtil.getDateTime(param.get("friTime"));
			ForumSectionInfoTreeManage forumSectionInfoTreeManage = this.lookupService(ForumSectionInfoTreeManage.class);
			forumSectionInfoTreeManage.updateFriIssharepoint(friId,"Y");
			forumSectionInfoTreeManage.updateFriTime(friId,friTime);
			responseText = SUCCESS;
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return new AjaxRenderer(responseText);
	}
	
	@PageAction
	public ViewRenderer moveForumPostMessage(DataParam param) {
		String responseText = FAIL;
		try {
			String fpmId = param.get("fpmId");
			String sectionId = param.get("sectionId");
			ForumSectionInfoTreeManage forumSectionInfoTreeManage = this.lookupService(ForumSectionInfoTreeManage.class);
			forumSectionInfoTreeManage.updatePostSctionRel(fpmId,sectionId);
			
			responseText = SUCCESS;
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return new AjaxRenderer(responseText);
	}
}